package com.server;

import com.service.HelloImpl;
import com.service.IHello;
import com.utils.CommonUtils;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
 * Created by lyh on 2017/7/17.
 */
public class MyServer {

    public static void main(String[] args) throws AlreadyBoundException, MalformedURLException {

        try {
            //创建一个远程对象
            IHello hello = new HelloImpl();
            int port = 8888;
            //本地主机上的远程对象注册表Registry的实例，并指定端口为8888，这一步必不可少（Java默认端口是1099）
            LocateRegistry.createRegistry(port);
            //把远程对象注册到RMI注册服务器上，并命名为RHello
            Naming.bind("rmi://"+ CommonUtils.getPcIp()+":"+port+"/RHello",hello);
            System.out.println("远程IHello对象绑定成功！");
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }
}
